var user=getUserEntityFromCache();
var from_center =parseInt(getParameter("from_center"));//来自个人中心
var from_partner =parseInt(getParameter("from_partner"));//来自合伙人界面
var partner_id =parseInt(getParameter("partner_id"));//合伙人的邀请人id，0代表平台，注册一级合伙人，//直接从公众号菜单过来时，partner_id=-1
var grade =parseInt(getParameter("grade"));
var isRequest=0;


function initData() {
    if(isNaN(from_center))
    {
        from_center=0;
    }

    if(isNaN(from_partner))
    {
        from_partner=0;
    }

    if(isNaN(partner_id))
    {
        partner_id=0;
    }

    if(isNaN(grade))
    {
        grade=0;
    }
}

function addEvent() {
    $("#image").click(function () {
        if(parseInt($("#image").attr("data-enable"))>0) {
            var str = $("#number").val();
            if (str.length < 11 || !isValidMobile(str)) {
                showAlert("请输入正确的手机号");
            } else {
                if(isRequest==0){
                    getMsgCode($.trim(str), function (m_code) {
                        $("#number").val("");

                        window.location.href = "/user/mobilecode?mobile="+$.trim(str)+"&from_center="+from_center+"&from_partner="+from_partner+"&partner_id="+partner_id+"&grade="+grade;
                    });
                }
                else {
                    showAlert("正在请求验证码，请不要频繁点击");
                }
            }
        }
        else {
            showAlert("请输入正确的手机号");
        }
    });

    $("#number").keyup(function () {
        var inputStr=$("#number").val();
        if(inputStr.length>=11){
            $("#image").attr({
                "src":"../images/btn_info_nextOn.png",
                "data-enable":"1"
            })
        }else {
            $("#image").attr({
                "src":"../images/btn_info_next.png",
                "data-enable":"0"
            })
        }
    });
}

function getWxUserInfo() {
    getUserInfo(function (data) {
        if(data&&data.openid){
            user=new User(data.openid,data.mobile,data.nickname,data.headimgurl,data.uid,data.role,data.referee,data.parent_code,data.redpacket_money,data.redpacket_num,data.isgetredpacket);
            user.cacheUserInfo(user.openid,user.nickname,user.mobile,user.avatar,user.uid,data.role,data.referee,data.parent_code,data.redpacket_money,data.redpacket_num,data.isgetredpacket);
            if(user.mobile!=undefined&&user.mobile!=null&&user.mobile.toString().length>0){
                if(parseInt(from_center)>0){
                    window.location.href="/user/index";
                }
                else {
                    window.location.href="/home";
                }
            }
        }
        else {
            showAlert(data);
        }
    });
}

function getMsgCode(mobile,callback){
    if(mobile!=undefined&&mobile!=null&&mobile.length>0&&user.uid){
        isRequest=1;
        $.ajax({
            type: "post",
            async: true,
            url: "/v1/wx-user/send-msg",
            data: {mobile:mobile,uid:user.uid},
            dataType: "json",
            success: function(data){
                isRequest=0;
                if(data&&data.code&&data.code=="success"){
                    if(callback){
                        var sms_code=data.sms_code;
                        callback(sms_code);

                        alert("验证码已发送");
                    }
                }
                else {
                    showAlert(data.message);
                }
            },
            error: function(){
                isRequest=0;
                showAlert("请求失败");
            }
        });
    }
    else {
        showAlert("参数不正确");
        isRequest=0;
    }
};


$(function () {
    initData();
    addEvent();
    if (!user.isLogin()) {//没登录
        //微信授权回调回来
        if(code!=undefined&&code!=null&&code.length>0&&state=="pocket"){
            getWxUserInfo();
        }
        else {
            user.clearUserInfoCache();
            getUserAuthorize();
        }
    }
    else {
        if(user.mobile!=undefined&&user.mobile!=null&&user.mobile.toString().length>0){
            if(parseInt(from_center)>0){
                window.location.href="/user/index";
            }
            else {
                window.location.href="/home";
            }
        }
    }
});